package com.wwp.tbshop.view.fragment;


import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wwp.tbshop.R;
import com.wwp.tbshop.accout_center.presenter.ExitUser_presenter;
import com.wwp.tbshop.accout_center.presenter.GetUeserId_presenter;
import com.wwp.tbshop.activity.AddressManageActivity;
import com.wwp.tbshop.activity.CollectGoodsActivity;
import com.wwp.tbshop.activity.RegisterOrLoginActivity;
import com.wwp.tbshop.activity.orderlist.OrderListActivity;
import com.wwp.tbshop.model.bean.ExitBean;
import com.wwp.tbshop.model.bean.UserInfoBean;
import com.wwp.tbshop.util.SPUtils;
import com.wwp.tbshop.util.UiUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 楠GG on 2017/6/15.
 */

public class UserFragment extends Fragment {

    public GetUeserId_presenter mGetUeserId_presenter;


    public ExitUser_presenter mExitUser_presenter;
    @Bind(R.id.back)
    ImageView mBack;
    @Bind(R.id.message)
    ImageView mMessage;
    @Bind(R.id.tv_user_setting)
    ImageView mTvUserSetting;
    @Bind(R.id.exit)
    ImageView mExit;
    @Bind(R.id.no_login_head)
    ImageView mNoLoginHead;
    @Bind(R.id.username1)
    TextView mUsername1;
    @Bind(R.id.phone)
    TextView mPhone;
    @Bind(R.id.no_login_item)
    LinearLayout mNoLoginItem;
    @Bind(R.id.no_login)
    LinearLayout mNoLogin;
    @Bind(R.id.imageView)
    ImageView mImageView;
    @Bind(R.id.username)
    TextView mUsername;
    @Bind(R.id.member)
    TextView mMember;
    @Bind(R.id.integal)
    TextView mIntegal;
    @Bind(R.id.alread_login)
    RelativeLayout mAlreadLogin;
    @Bind(R.id.order_back)
    ImageView mOrderBack;
    @Bind(R.id.my_order)
    LinearLayout mMyOrder;
    @Bind(R.id.pay01)
    RadioButton mPay01;
    @Bind(R.id.pay02)
    RadioButton mPay02;
    @Bind(R.id.pay03)
    RadioButton mPay03;
    @Bind(R.id.pay04)
    RadioButton mPay04;
    @Bind(R.id.pay05)
    RadioButton mPay05;
    @Bind(R.id.address)
    TextView mAddress;
    @Bind(R.id.rl_address)
    RelativeLayout mRlAddress;
    @Bind(R.id.shoucangjia)
    RelativeLayout mShoucangjia;
    @Bind(R.id.love)
    RelativeLayout mLove;
    @Bind(R.id.shop)
    RelativeLayout mShop;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        View inflate = View.inflate(getActivity(), R.layout.user_activity, null);
        mExitUser_presenter = new ExitUser_presenter(this);
        initConnect();
        ButterKnife.bind(this, inflate);
        return inflate;
    }


    private void initConnect() {
        mGetUeserId_presenter = new GetUeserId_presenter(this);
        // 进入账号中心   保存了userid ---> 用户界面
        //                没有保存 --》 登录 和 注册页面
        // 查询本地 有没userid

        String string = SPUtils.getString(UiUtil.sContext, SPUtils.USERID, null);

        if (!TextUtils.isEmpty(string)) {
            // 有保存 useractivity 用户中心
            mGetUeserId_presenter.loadUserNewsInfo(string);
        }
/*        else {
            //没有保存  登录注册页面 register_login activity
            Intent intent3 = new Intent(getActivity(), RegisterOrLoginActivity.class);
            getActivity().startActivity(intent3);
        }*/


    }

    public void successForBack(UserInfoBean userInfoBean) {
        // 避免 回退按钮  framgent被finish
        if (mUsername == null||userInfoBean ==null) {
            return;
        }

       // 数据请求成功
        String bonus = userInfoBean.getUserInfo().getBonus();
        String level = userInfoBean.getUserInfo().getLevel();
        String userid = userInfoBean.getUserInfo().getUserid();
        mUsername.setText("用户ID：     " + userid);
        mMember.setText(level);
        mIntegal.setText("      " + "积分:" + bonus);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


    // 退出当前 账号
    public void exit() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        builder.setTitle("确定要退出当前账户？");
        builder.setPositiveButton("确定", new AlertDialog.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                // 服务器 注销用户
                String string = SPUtils.getString(UiUtil.sContext, SPUtils.USERID, null);
                mExitUser_presenter.ExitUserNewsInfo(string);

                // 本地删除用户id
                SPUtils.putString(UiUtil.sContext, SPUtils.USERID, null);

                // 收起 对话框
                dialog.dismiss();

                // 退出后 进入 注册页面
                Intent intent = new Intent(getActivity(), RegisterOrLoginActivity.class);
                getActivity().startActivity(intent);

            }
        }).setNegativeButton("我还要逛逛", new AlertDialog.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                // 收起 对话框
                dialog.dismiss();

            }
        });
        AlertDialog dialog = builder.show();
    }


    // 登录 注册完 数据回显
    @Override
    public void onResume() {
        super.onResume();

        String string = SPUtils.getString(UiUtil.sContext, SPUtils.USERID, null);
        if (!TextUtils.isEmpty(string)) {
            mGetUeserId_presenter.loadUserNewsInfo(string);
            login();
        } else {
            no_login();
        }
    }

    public void onSuccess(ExitBean exitBean) {
        Log.d("exit", "注销成功onSuccess: " + exitBean.getResponse());
        TextView textView = new TextView(UiUtil.getContext());
        textView.setText("我的");
        textView.setTextColor(Color.BLACK);
        textView.setGravity(Gravity.CENTER);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction("OrderList");
                startActivity(intent);
            }
        });
    }

    //登录状态
    public void login() {
        mNoLogin.setVisibility(View.GONE);
        mAlreadLogin.setVisibility(View.VISIBLE);
    }

    //未登录状态
    public void no_login() {
        mNoLogin.setVisibility(View.VISIBLE);
        mAlreadLogin.setVisibility(View.GONE);
    }


    @OnClick({R.id.back, R.id.message, R.id.tv_user_setting, R.id.exit, R.id.no_login_head, R.id.username1,
            R.id.phone, R.id.no_login_item, R.id.no_login, R.id.imageView, R.id.username, R.id.member,
            R.id.integal, R.id.alread_login, R.id.order_back, R.id.my_order, R.id.pay01, R.id.pay02, R.id.pay03,
            R.id.pay04, R.id.pay05, R.id.address, R.id.rl_address, R.id.shoucangjia, R.id.love, R.id.shop})
    public void onViewClicked(View view) {
        switch (view.getId()) {


            case R.id.no_login_head:
                //调到登录 注册页面
                Intent intent = new Intent(getActivity(), RegisterOrLoginActivity.class);
                startActivity(intent);
                break;

            case R.id.no_login_item:
                //调到登录 注册页面
                Intent intent2 = new Intent(getActivity(), RegisterOrLoginActivity.class);
                startActivity(intent2);
                break;

            case R.id.back:
                //回首页
                getActivity().onBackPressed();
                break;

            case R.id.message:
                //动态信息
                break;
            case R.id.tv_user_setting:
                //设置
                break;
            case R.id.exit:
                // 没登录情况
                String string = SPUtils.getString(UiUtil.sContext, SPUtils.USERID, null);
                if (TextUtils.isEmpty(string)) {
                    Toast.makeText(getActivity(), "你还没登录，亲", Toast.LENGTH_SHORT).show();
                    return;
                }
                // 退出当前账号
                exit();
                break;
            case R.id.no_login:
                //调到登录 注册页面
                Intent intent1 = new Intent(getActivity(), RegisterOrLoginActivity.class);
                startActivity(intent1);
                break;
            case R.id.imageView:
                // 登录头像 设置
                break;

            case R.id.alread_login:
                // 已登录的用户信息
                break;
            case R.id.my_order:
                // 我的订单
                Intent intent3 = new Intent(getActivity(), OrderListActivity.class);
                getActivity().startActivity(intent3);
                break;
            case R.id.pay01:
                //代付款
                break;
            case R.id.pay02:
                //代发货
                break;
            case R.id.pay03:
                //代评价
                break;
            case R.id.pay04:
                //退换/换货
                break;
            case R.id.pay05:
                //我的钱包
                break;

            case R.id.address:
                //地址管理
                into_addressManger();
            break;
            case R.id.shoucangjia:
                //收藏夹
                //Toast.makeText(getActivity(), "收藏夹，亲", Toast.LENGTH_SHORT).show();
                into_CollectGoods();
                break;
            case R.id.love:
                //猜你喜欢

                break;
            case R.id.shop:
                //商品收藏

                break;
        }


    }

    private void into_CollectGoods() {
        if (!isLoading()) {
            Toast.makeText(getActivity(), "还没登录哦，亲", Toast.LENGTH_SHORT).show();
            return;
        }
        Intent intent = new Intent(getActivity(), CollectGoodsActivity.class);
        getActivity().startActivity(intent);
    }


    private void into_addressManger() {
        if (!isLoading()) {
            Toast.makeText(getActivity(), "还没登录哦，亲", Toast.LENGTH_SHORT).show();
            return;
        }
        Intent intent4 = new Intent(getActivity(), AddressManageActivity.class);
        getActivity().startActivity(intent4);
    }

    // 是否已登录
    public boolean isLoading() {
        String uerid1 = SPUtils.getUerid();
        if (TextUtils.isEmpty(uerid1)) {
            return false;
        }
        return true;
    }
}
